package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		ArrayList<JSONObject> list = new ArrayList<>();
		//先查询所有的分类数据：一级、二级、三级
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//将这个List集合数据转为Map集合
		Map<Long, List<BaseCategoryView>> map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//循环遍历当前map集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()){
			//获取迭代器中的数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();

			//获取key与value
			Long category1Id = entry.getKey();
			List<BaseCategoryView> categoryViewList = entry.getValue();
			//创建一个类JSONObject
			JSONObject category1 = new JSONObject();
			//想办法查询数据并赋值
			category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
			category1.put("categoryId",category1Id);
			//声明一个集合来存储二级分类对象
			ArrayList<JSONObject> category2ChildList = new ArrayList<>();
			//必须知道当前一级分类对应的二级分类的数据！
			Map<Long, List<BaseCategoryView>> map1 = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			while (iterator1.hasNext()){
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				//获取二级分类Id
				Long category2Id = entry1.getKey();
				//获取二级分类的集合数据
				List<BaseCategoryView> viewList = entry1.getValue();
				//创建一个类JSONObject二级对象
				JSONObject category2 = new JSONObject();
				//获取分类Id
				category2.put("categoryName",viewList.get(0).getCategory2Name());
				category2.put("categoryId",category2Id);
				//获取三级分类数据
				List<JSONObject> category3List = viewList.stream().map(baseCategoryView -> {
					//创建一个类JSONObject三级对象
					JSONObject category3 = new JSONObject();
					Long category3Id = baseCategoryView.getCategory3Id();
					String category3Name = baseCategoryView.getCategory3Name();
					category3.put("categoryName", category3Name);
					category3.put("categoryId", category3Id);
					return category3;
				}).collect(Collectors.toList());


				category2.put("categoryChild",category3List);
				category2ChildList.add(category2);
			}
			category1.put("categoryChild",category2ChildList);
			//编写实体类：
			list.add(category1);
		}
		//返回数据
		return list;

	}
}
